package c.j.a.d.b.e;

import android.app.AlarmManager;
import android.app.Application;
import android.content.Context;
import android.content.IntentFilter;
import android.text.TextUtils;
import c.j.a.c.l;
import c.j.a.d.b.b.d;
import c.j.a.d.b.d.a0;
import c.j.a.d.b.d.e;
import c.j.a.d.b.d.f;
import c.j.a.d.b.d.j;
import c.j.a.d.b.d.n;
import c.j.a.d.b.d.y;
import c.j.a.d.b.i.g;
import c.j.a.d.b.i.h;
import c.j.a.d.b.i.i;
import c.j.a.d.b.j.c;
import c.j.a.d.b.j.k;
import c.j.a.d.b.j.m;
import c.j.a.d.b.j.o;
import c.j.a.d.b.j.q;
import c.j.a.d.b.j.s;
import com.ss.android.socialbase.downloader.downloader.DownloadReceiver;
import f.v;
import f.w;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
import org.json.JSONObject;

/* compiled from: DownloadComponentManager */
public class b {
    public static volatile List<n> A = new ArrayList();
    public static volatile boolean B = false;
    public static volatile v C = null;
    public static volatile AlarmManager D;
    public static boolean E = false;
    public static int F;
    public static final int G;
    public static final int H = ((Runtime.getRuntime().availableProcessors() * 2) + 1);
    public static final int I;
    public static final int J;
    public static int K = 8192;
    public static boolean L;
    public static final List<y> M = new ArrayList();
    public static final List<f> N = new ArrayList();
    public static int O;
    public static boolean P = true;
    public static l Q;
    public static volatile boolean R = false;
    public static volatile Context a;
    public static volatile n b;
    public static volatile c.j.a.d.b.j.n c;
    public static volatile l d;
    public static volatile j e;
    public static volatile c f;
    public static volatile r g;
    public static volatile a0 h;
    public static volatile r i;
    public static volatile i j;
    public static volatile g k;
    public static volatile i l;
    public static volatile g m;
    public static volatile o n;
    public static volatile ExecutorService o;
    public static volatile ExecutorService p;
    public static volatile ExecutorService q;
    public static volatile ExecutorService r;
    public static volatile ExecutorService s;
    public static volatile ExecutorService t;
    public static volatile ExecutorService u;
    public static volatile ExecutorService v;
    public static volatile k w;
    public static volatile DownloadReceiver x;
    public static volatile s y;
    public static volatile e z;

    /* compiled from: DownloadComponentManager */
    public static class a implements Runnable {
        public void run() {
            Context a = b.a();
            if (a != null) {
                c.j.a.d.b.n.a.K(a);
            }
        }
    }

    static {
        int availableProcessors = Runtime.getRuntime().availableProcessors() + 1;
        G = availableProcessors;
        I = availableProcessors;
        J = availableProcessors;
    }

    public static c.j.a.d.b.j.n A() {
        if (c == null) {
            synchronized (b.class) {
                if (c == null) {
                    c = new c.j.a.d.b.j.n();
                }
            }
        }
        return c;
    }

    public static c B() {
        if (f == null) {
            synchronized (b.class) {
                if (f == null) {
                    f = new c.j.a.d.b.j.j();
                }
            }
        }
        return f;
    }

    public static JSONObject C() {
        if (z == null || z.a() == null) {
            return c.j.a.d.b.b.e.d;
        }
        JSONObject a = z.a();
        l(a);
        return a;
    }

    public static synchronized Context a() {
        Context context;
        synchronized (b.class) {
            context = a;
        }
        return context;
    }

    public static int b(com.ss.android.socialbase.downloader.g.c cVar) {
        if (cVar == null) {
            return 0;
        }
        return c(cVar.d, cVar.e);
    }

    public static int c(String str, String str2) {
        c.j.a.d.b.j.n A = A();
        if (A == null) {
            return 0;
        }
        return A.a(str, str2);
    }

    public static c.j.a.d.b.i.f d(String str, List<com.ss.android.socialbase.downloader.g.e> list, int i, boolean z, com.ss.android.socialbase.downloader.g.c cVar) throws Exception {
        int[] m = m(i);
        int length = m.length;
        int i2 = 0;
        Exception e = null;
        while (i2 < length) {
            try {
                str = n(str, list, m[i2], z, cVar);
                return str;
            } catch (Exception e2) {
                e = e2;
                i2++;
            }
        }
        if (e == null) {
            return null;
        }
        throw e;
    }

    public static h e(int i, String str, List<com.ss.android.socialbase.downloader.g.e> list, int i2, boolean z, com.ss.android.socialbase.downloader.g.c cVar) throws com.ss.android.socialbase.downloader.e.a, IOException {
        i iVar;
        Throwable th;
        IOException iOException;
        if (i2 == 1) {
            iVar = j;
        } else {
            if (l == null) {
                synchronized (b.class) {
                    if (l == null) {
                        l = new c.j.a.d.b.j.l();
                    }
                }
            }
            iVar = l;
        }
        if (iVar != null) {
            long j = 0;
            if (z) {
                try {
                    j = System.currentTimeMillis();
                } catch (IOException e) {
                    throw e;
                } catch (Throwable th2) {
                    iOException = e;
                    th = th2;
                }
            }
            h a = iVar.a(i, str, list);
            if (z) {
                c.j.a.c.y.a.i.o(a, str, System.currentTimeMillis() - j, "get", i2, null, cVar);
            }
            return a;
        }
        throw new com.ss.android.socialbase.downloader.e.a(1022, new IOException(c.b.a.a.a.A("httpService not exist, netLib = ", i2)));
        if (z) {
            c.j.a.c.y.a.i.o(null, str, System.currentTimeMillis() - 0, "get", i2, iOException, cVar);
        }
        throw th;
    }

    public static h f(boolean z, int i, String str, List<com.ss.android.socialbase.downloader.g.e> list, int i2, boolean z2, com.ss.android.socialbase.downloader.g.c cVar) throws Exception {
        if (!z) {
            i2 = 2;
        }
        int[] m = m(i2);
        i2 = m.length;
        int i3 = 0;
        Exception e = null;
        while (i3 < i2) {
            try {
                h e2 = e(i, str, list, m[i3], z2, cVar);
                if (e2 != null) {
                    return e2;
                }
                i3++;
            } catch (Exception e3) {
                e = e3;
            }
        }
        if (e == null) {
            return null;
        }
        throw e;
    }

    public static synchronized void g(Context context) {
        synchronized (b.class) {
            if (context != null) {
                if (a == null) {
                    a = context.getApplicationContext();
                    c.j.a.d.b.a.a aVar = c.j.a.d.b.a.a.c.a;
                    Context context2 = a;
                    if (aVar.a == null && (context2 instanceof Application)) {
                        synchronized (aVar) {
                            if (aVar.a == null) {
                                Application application = (Application) context2;
                                aVar.a = application;
                                application.registerActivityLifecycleCallbacks(aVar.e);
                            }
                        }
                    }
                }
            }
        }
    }

    public static void h(d dVar) {
        List<y> list = M;
        synchronized (list) {
            for (y yVar : list) {
                if (yVar != null) {
                    if (dVar == d.SYNC_START) {
                        yVar.a();
                    } else if (dVar == d.SYNC_SUCCESS) {
                        yVar.b();
                    }
                }
            }
            M.clear();
        }
    }

    public static synchronized void i(j jVar) {
        synchronized (b.class) {
            if (R) {
                c.j.a.d.b.g.a.g("DownloadComponentManager", "component has init");
                return;
            }
            boolean z = B;
            if (jVar != null) {
                g(jVar.a);
                j jVar2 = jVar.b;
                if (jVar2 != null) {
                    e = jVar2;
                }
                i iVar = jVar.c;
                if (iVar != null) {
                    j = iVar;
                }
                L = j != null;
                j(null);
                a0 a0Var = jVar.e;
                if (h == null) {
                    h = a0Var;
                }
                k kVar = jVar.d;
                if (kVar != null) {
                    w = kVar;
                }
                O = jVar.g;
                e eVar = jVar.f;
                z = eVar;
                c.j.a.d.b.l.a.h();
                l(eVar.a());
                P = true;
            }
            if (b == null) {
                b = new c.j.a.d.b.j.h();
            }
            if (g == null) {
                g = new m();
            }
            if (i == null) {
                i = new s();
            }
            if (c == null) {
                c = new c.j.a.d.b.j.n();
            }
            if (f == null) {
                f = new c.j.a.d.b.j.j();
            }
            if (d == null) {
                d = new c.j.a.d.b.j.g();
            }
            if (w == null) {
                w = new c.j.a.d.b.j.f();
            }
            if (y == null) {
                y = new o();
            }
            int i = F;
            if (i <= 0 || i > G) {
                F = G;
            }
            if (x == null) {
                x = new DownloadReceiver();
            }
            if (!E) {
                try {
                    IntentFilter intentFilter = new IntentFilter();
                    intentFilter.addAction("android.net.conn.CONNECTIVITY_CHANGE");
                    a.registerReceiver(x, intentFilter);
                    E = true;
                } catch (Throwable th) {
                    th.printStackTrace();
                }
            }
            if (B && !z && !c.j.a.d.b.n.a.M()) {
                q.a(true).d();
            } else if (c.j.a.d.b.n.a.Q()) {
                ExecutorService r = r();
                if (r != null) {
                    r.execute(new a());
                }
            } else {
                Context a = a();
                if (a != null) {
                    c.j.a.d.b.n.a.K(a);
                }
            }
            R = true;
        }
    }

    public static synchronized void j(o oVar) {
        synchronized (b.class) {
            if (oVar != null) {
                n = oVar;
                if (b instanceof c.j.a.d.b.j.h) {
                    ((c.j.a.d.b.j.h) b).p();
                }
            }
        }
    }

    public static void k(Runnable runnable, boolean z) {
        if (!z || c.j.a.d.b.n.a.Q()) {
            q().execute(runnable);
        } else {
            runnable.run();
        }
    }

    public static void l(JSONObject jSONObject) {
        if (jSONObject != null && TextUtils.isEmpty(c.j.a.d.b.b.e.c)) {
            c.j.a.d.b.b.e.c = jSONObject.optString("kllk");
            if (!TextUtils.isEmpty(c.j.a.d.b.b.e.c)) {
                c.j.a.d.b.b.e.b = c.j.a.d.b.b.e.c.toUpperCase();
            }
        }
    }

    public static int[] m(int i) {
        if (i == 1) {
            return new int[]{0};
        } else if (i == 2) {
            return new int[]{1};
        } else if (i != 3) {
            return new int[]{1, 0};
        } else {
            return new int[]{0, 1};
        }
    }

    public static c.j.a.d.b.i.f n(String str, List<com.ss.android.socialbase.downloader.g.e> list, int i, boolean z, com.ss.android.socialbase.downloader.g.c cVar) throws com.ss.android.socialbase.downloader.e.a, IOException {
        g gVar;
        long j;
        Throwable th;
        IOException iOException;
        if (i == 1) {
            gVar = k;
        } else {
            if (m == null) {
                synchronized (b.class) {
                    if (m == null) {
                        m = new k();
                    }
                }
            }
            gVar = m;
        }
        if (gVar != null) {
            j = 0;
            if (z) {
                try {
                    j = System.currentTimeMillis();
                } catch (IOException e) {
                    throw e;
                } catch (Throwable th2) {
                    iOException = e;
                    th = th2;
                }
            }
            c.j.a.d.b.i.f a = ((k) gVar).a(str, list);
            if (z) {
                c.j.a.c.y.a.i.o(a, str, System.currentTimeMillis() - j, "head", i, null, cVar);
            }
            return a;
        }
        throw new com.ss.android.socialbase.downloader.e.a(1022, new IOException(c.b.a.a.a.A("httpService not exist, netLib = ", i)));
        if (z) {
            c.j.a.c.y.a.i.o(null, str, System.currentTimeMillis() - j, "head", i, iOException, cVar);
        }
        throw th;
    }

    public static void o(Runnable runnable) {
        if (c.j.a.d.b.n.a.Q()) {
            u().execute(runnable);
        } else {
            runnable.run();
        }
    }

    public static AlarmManager p() {
        if (D == null) {
            synchronized (b.class) {
                if (D == null && a != null) {
                    D = (AlarmManager) a.getSystemService("alarm");
                }
            }
        }
        return D;
    }

    public static ExecutorService q() {
        if (o == null) {
            synchronized (b.class) {
                if (o == null) {
                    int i = G;
                    ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(i, i, 15, TimeUnit.SECONDS, new LinkedBlockingQueue(), new c.j.a.d.b.m.a("DownloadThreadPool-cpu-fixed", true));
                    try {
                        threadPoolExecutor.allowCoreThreadTimeOut(true);
                    } catch (Throwable th) {
                        th.printStackTrace();
                    }
                    o = threadPoolExecutor;
                }
            }
        }
        return o;
    }

    public static ExecutorService r() {
        return p != null ? p : q();
    }

    public static ExecutorService s() {
        if (q == null) {
            synchronized (b.class) {
                if (q == null) {
                    int i = I;
                    ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(i, i, 15, TimeUnit.SECONDS, new LinkedBlockingQueue(), new c.j.a.d.b.m.a("DownloadThreadPool-mix-fixed", true));
                    try {
                        threadPoolExecutor.allowCoreThreadTimeOut(true);
                    } catch (Throwable th) {
                        th.printStackTrace();
                    }
                    q = threadPoolExecutor;
                }
            }
        }
        return q;
    }

    public static ExecutorService t() {
        if (u == null) {
            synchronized (b.class) {
                if (u == null) {
                    int i = H;
                    ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(i, i, 15, TimeUnit.SECONDS, new LinkedBlockingQueue(), new c.j.a.d.b.m.a("DownloadThreadPool-chunk-fixed", true));
                    try {
                        threadPoolExecutor.allowCoreThreadTimeOut(true);
                    } catch (Throwable th) {
                        th.printStackTrace();
                    }
                    u = threadPoolExecutor;
                }
            }
        }
        return u;
    }

    public static ExecutorService u() {
        if (t == null) {
            synchronized (b.class) {
                if (t == null) {
                    int i = J;
                    ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(i, i, 15, TimeUnit.SECONDS, new LinkedBlockingQueue(), new c.j.a.d.b.m.a("DownloadThreadPool-db-fixed", true));
                    try {
                        threadPoolExecutor.allowCoreThreadTimeOut(true);
                    } catch (Throwable th) {
                        th.printStackTrace();
                    }
                    t = threadPoolExecutor;
                }
            }
        }
        return t;
    }

    public static v v() {
        if (C == null) {
            synchronized (b.class) {
                if (C == null) {
                    f.v.b bVar = new f.v.b();
                    TimeUnit timeUnit = TimeUnit.MILLISECONDS;
                    bVar.b(30000, timeUnit);
                    bVar.d(30000, timeUnit);
                    bVar.e(30000, timeUnit);
                    bVar.v = true;
                    bVar.u = true;
                    bVar.c(Collections.singletonList(w.HTTP_1_1));
                    if (v != null) {
                        bVar.a = new f.k(v);
                    }
                    C = new v(bVar);
                }
            }
        }
        return C;
    }

    public static synchronized o w() {
        o oVar;
        synchronized (b.class) {
            oVar = n;
        }
        return oVar;
    }

    public static n x() {
        if (b == null) {
            synchronized (b.class) {
                if (b == null) {
                    b = new c.j.a.d.b.j.h();
                }
            }
        }
        return b;
    }

    public static r y() {
        if (g == null) {
            synchronized (b.class) {
                if (g == null) {
                    g = new m();
                }
            }
        }
        return g;
    }

    public static r z() {
        if (i == null) {
            synchronized (b.class) {
                if (i == null) {
                    i = new s();
                }
            }
        }
        return i;
    }
}
